package qlsl.androiddesign.http.service.subservice;

import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import android.os.AsyncTask;
import qlsl.androiddesign.asynctask.subasynctask.JobAsyncTask;
import qlsl.androiddesign.constant.SoftwareConstant;
import qlsl.androiddesign.constant.WhatConstant;
import qlsl.androiddesign.entity.commonentity.Pager;
import qlsl.androiddesign.entity.otherentity.RecruitJob;
import qlsl.androiddesign.handler.subhandler.HttpHandler;
import qlsl.androiddesign.http.HttpListener;
import qlsl.androiddesign.http.service.baseservice.BaseService;
import qlsl.androiddesign.http.xutils.HttpProtocol;
import qlsl.androiddesign.thread.HandlerThread;
import qlsl.androiddesign.util.commonutil.PagerUtils;
import qlsl.androiddesign.view.baseview.FunctionView;

/**
 * 获取求职信息信息模块 <br/>
 * 
 */
public class JobService extends BaseService {

	private static String className = getClassName(JobService.class);

	/**
	 * 查询求职列表
	 * 
	 * @param pageindex
	 *            页码
	 * @param type
	 *            1 招聘 2求职
	 * @param city
	 *            城市
	 * @param functionView
	 * @param listener
	 */
	public static void queryJobList(final int pageindex, final String type,
			final String city, final FunctionView<?> functionView,
			final HttpListener listener) {
		functionView.setProgressBarText("正在查询");
		final HttpHandler handler = getHandler(functionView, listener,
				className, "queryJobList");
		new HandlerThread(className, "queryJobList") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("getadvertiseslist")
							.addParam("type", type)
							.addParam("pageindex", pageindex)
							.addParam("city", city)
							.addParam("pagesize", SoftwareConstant.PAGER_SIZE)
							.removeUserParam().post();

					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					HashMap<String, Object> map = new HashMap<String, Object>();
					List<RecruitJob> list = JSONArray.parseArray(
							jo.getString("rows"), RecruitJob.class);
					Pager pager = PagerUtils.createPager(pageindex, jo);
					map.put("list", list);
					map.put("city", city);
					map.put("pager", pager);

					handler.sendMessage(protocol,
							WhatConstant.WHAT_NET_DATA_SUCCESS, map);

				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON,
							e);
				}
			};
		}.start();
	}

	/**
	 * 查询我的求职列表
	 * 
	 * @param pageindex
	 *            页码
	 * @param type
	 *            1 招聘 2求职
	 * @param city
	 *            城市
	 * @param functionView
	 * @param listener
	 */
	public static void queryJobList(final int pageindex, final String type,
			final FunctionView<?> functionView, final HttpListener listener) {
		functionView.setProgressBarText("正在查询");
		final HttpHandler handler = getHandler(functionView, listener,
				className, "queryJobList");
		new HandlerThread(className, "queryJobList") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("getadvertiseslist")
							.addParam("type", type)
							.addParam("pageindex", pageindex)
							.addParam("pagesize", SoftwareConstant.PAGER_SIZE)
							.post();

					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					HashMap<String, Object> map = new HashMap<String, Object>();
					List<RecruitJob> list = JSONArray.parseArray(
							jo.getString("rows"), RecruitJob.class);
					Pager pager = PagerUtils.createPager(pageindex, jo);
					map.put("list", list);
					map.put("pager", pager);

					handler.sendMessage(protocol,
							WhatConstant.WHAT_NET_DATA_SUCCESS, map);

				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON,
							e);
				}
			};
		}.start();
	}

	private static AsyncTask<Void, Void, Void> mSaveCacheTask;

	/**
	 * 查询求职列表
	 * 
	 * @param pageindex
	 *            页码
	 * @param type
	 *            1 招聘 2求职
	 * @param city
	 *            城市
	 * @param functionView
	 * @param listener
	 */
	public static void queryJobList2(final int pageindex, final String type,
			final String city, final FunctionView<?> functionView,
			final HttpListener listener) {
		functionView.setProgressBarText("正在查询求职");
		final HttpHandler handler = getHandler(functionView, listener,
				className, "queryJobList");

		// cancelTask();
		mSaveCacheTask = new JobAsyncTask(type, pageindex, city, handler)
				.execute();

		/*
		 * new HandlerThread(className, "queryJobList") { public void run() {
		 * HttpProtocol protocol = new HttpProtocol(); try { JSONObject jo =
		 * protocol.setMethod("getadvertiseslist") .addParam("type", type)
		 * .addParam("pageindex", pageindex) .addParam("city", city)
		 * .addParam("pagesize", SoftwareConstant.PAGER_SIZE) .post();
		 * 
		 * Log.e("zcj", protocol.getParams());
		 * 
		 * Log.e("zcj", jo);
		 * 
		 * if (isDataInvalid(jo, handler, protocol)) { return; }
		 * 
		 * HashMap<String, Object> map = new HashMap<String, Object>();
		 * List<Recruit> list = JSONArray.parseArray( jo.getString("rows"),
		 * Recruit.class); Pager pager = PagerUtils.createPager(pageindex, jo);
		 * map.put("list", list);
		 * 
		 * map.put("pager", pager);
		 * 
		 * handler.sendMessage(protocol, WhatConstant.WHAT_NET_DATA_SUCCESS,
		 * map);
		 * 
		 * } catch (Exception e) { e.printStackTrace();
		 * handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON, e); } };
		 * }.start();
		 */
	}

	public static void cancelTask() {
		if (mSaveCacheTask != null) {
			mSaveCacheTask.cancel(true);
			mSaveCacheTask = null;
		}
	}

}
